<?php
/**
 * 上传类
 * User: Administrator
 * Date: 2018/7/21
 * Time: 13:49
 */

namespace app\admin\controller;

use OSS\Core\OssException;
use OSS\OssClient;
use think\Image;


class Upload  extends Init
{
    function _initialize()
    {
        parent::_initialize();
        $this->model = model('upload');
        $this->image = model('image');
    }

    public function upfile(){
        return json($this->model->upfile('files'));
    }

    public function upimage(){
        $id = input('param.id');
        $list = $this->model->upfile('images');

        if($list['code']==200){
            $data['img_url'] = $list['path'];
            if(!empty($id)){
                $where['img_id'] = $id;
                $this->image->del($where);
                $result = $this->image->edit($data,$where);
                if($result){
                    return json(array('code'=>200,'msg'=>'添加成功'));
                }else{
                    return json(array('code'=>0,'msg'=>'添加失败'));
                }
            }
            $result = $this->image->add($data);
            if($result){
                return json(array('code'=>200,'msg'=>'添加成功'));
            }else{
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
        }

    }





    public function upimages(){
        $id = input('param.id');
        $list = $this->model->upfile('images');
        if($list['code']==200){
            $data['img_url'] = $list['path'];
            if(!empty($id)){
                $where['img_id'] = $id;
                $this->image->del($where);
                $result = $this->image->edit($data,$where);
                if($result){
                    return json(array('code'=>200,'msg'=>'添加成功'));
                }else{
                    return json(array('code'=>0,'msg'=>'添加失败'));
                }
            }

            return json(array('code'=>200,'msg'=>'添加成功','data'=>$list));

        }else{
            return json(array('code'=>0,'msg'=>'添加失败'));
        }

    }



    public function upimage_s(){
        $list = $this->model->upfile('images');
        if($list['code']==200){
            return json(array('code'=>200,'msg'=>'添加成功','data'=>$list));
        }else{
            return json(array('code'=>0,'msg'=>'添加失败'));
        }

    }


    //上传到阿里云oss
    public function uploadFile()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'goods/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }

        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>$result['info']['url']));
        return json($data);

    }

    //上传到阿里云oss,详情图
    public function uploadFiles()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'goods/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }
        //将结果输出
        $data = array('code'=>0,'msg'=>'上传成功','data'=>array('src'=>$result['info']['url']));
        return json($data);
//        return array('code'=>200,'msg'=>'上传成功','path'=>$result['info']['url']);

    }


    //上传到阿里云oss,会员卡图片
    public function uploadCard()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'card/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }
        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>'/'.$fileName));
        return json($data);
    }

    //上传到阿里云oss,轮播图片
    public function uploadImage()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'ad/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }
        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>'/'.$fileName));
        return json($data);
    }

    //上传到阿里云oss,磨损图片
    public function abrasion()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'abrasion/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }

        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>$result['info']['url']));
        return json($data);
    }


    //上传到阿里云oss,首页导航
    public function navig()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'navig/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }

        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>'/'.$fileName));
        return json($data);
    }

    //上传到阿里云oss,首页导航
    public function games()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'games/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }

        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>'/'.$fileName));
        return json($data);
    }

    //上传到阿里云oss,支付凭证
    public function voucher()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'voucher/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }

        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>$result['info']['url']));
        return json($data);
    }

    //上传到阿里云oss,主题
    public function theme()
    {
        vendor('aliyun.autoload');
        $file = request()->file('file');  //获取到上传的文件
        $resResult = Image::open($file);
        // 尝试执行
        try {
            $config = config('aliyun_oss'); //基本配置
            //实例化对象 将配置传入

            $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'theme/'.date('Ym').'/'.sha1(date('YmdHis', time()) . uniqid()) . '.' . $resResult->type();

            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file->getInfo()['tmp_name']);
        } catch (OssException $e) {
            return $e->getMessage();
        }

        //将结果输出
        $data = array('code'=>200,'msg'=>'上传成功','data'=>array('src'=>'/'.$fileName));
        return json($data);
    }
}